package com.beehoo.dcportal.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

public class CommonUtils {
	
	public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:m:ss");
	public static SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmssSSS");

	public static String nvlToString(Object o){
		String ret = o == null ? "":String.valueOf(o);
		return ret.trim();
	}
	
	/**
	 * 获取IP
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getDate() {
        return format.format(new Date());
    }
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getDateTime() {
        return format1.format(new Date());
    }
	
	/**
	 * 获取过去的时间
	 * @param day
	 * @return
	 */
	public static String getPastDay(int day) {
    	Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -day);
        return format.format(calendar.getTime());
    }
	
	/**
	 * 转换信息状态
	 * @param value
	 * @return
	 */
	public static String getMessageState(String value){
		String ret = "已保存";
		value = CommonUtils.nvlToString(value);
		if(value.equals("0")){
			ret = "已保存";
		}else if(value.equals("1")){
			ret = "未审核";
		}else if(value.equals("2")){
			ret = "已驳回";
		}else if(value.equals("3")){
			ret = "已发布";
		}
		return ret;
	}
	
	/**
	 * 生成信息图片文件名
	 * @param streetId
	 * @param flag
	 * @return
	 */
	public static String getUploadFileName(String streetId,String flag){
		StringBuffer buf = new StringBuffer();
		buf.append(streetId);
		buf.append(flag);
		buf.append(format2.format(new Date()));
		buf.append(".jpg");
		return buf.toString();
	}
	/**
	 * 生成信息附件文件名
	 * @param streetId
	 * @param flag
	 * @return
	 */
	public static String getUploadAttachmentName(String suffix){
		StringBuffer buf = new StringBuffer();
		buf.append(format2.format(new Date()));
		buf.append(suffix);
		return buf.toString();
	}
	/**
	 * 校验是否是图片文件
	 * @param contentType
	 * @return
	 */
	public static boolean validateImageFileType(String contentType){
		boolean result = false;
		if(contentType.equalsIgnoreCase("image/bmp")
				|| contentType.equalsIgnoreCase("image/png")
				|| contentType.equalsIgnoreCase("image/x-png")
				|| contentType.equalsIgnoreCase("image/gif")
				|| contentType.equalsIgnoreCase("image/jpeg")
				|| contentType.equalsIgnoreCase("image/pjpeg")
				|| contentType.equalsIgnoreCase("image/jpg")){
			result = true;
		}
		return result;
	}
}
